package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/25 20:51
 */
public class C12 {


    public static String intToRoman(int num) {
        StringBuilder sb = new StringBuilder();
        int remainder = num % 10;
        String[] romanStr = {"I", "V", "X", "L", "C", "D", "M"};
        int i = 0;
        for (; num > 0; num /= 10, remainder = num % 10, i += 2) {
            if (remainder == 0) continue;
            if (remainder == 4) sb.insert(0, romanStr[i] + romanStr[i + 1]);
            else if (remainder == 9) sb.insert(0, romanStr[i] + romanStr[i + 2]);
            else if (remainder < 5) sb.insert(0, romanStr[i].repeat(remainder));
            else {     // >= 5
                sb.insert(0, romanStr[i + 1] + romanStr[i].repeat(remainder - 5));
            }
        }
        return sb.toString();
    }


    // for test
    public static void main(String[] args) {
        int num = 10;
        System.out.println(intToRoman(num));
    }


}
